#include "uithread.hpp"
#include "app_config.h"
#include "variable.hpp"
#include "lcd_cmf1p2301.hpp"
#include "drawing.hpp"
#include "keyboard.hpp"

#include "threados.hpp"
#include "method.hpp"
#include "streamstation.hpp"

#include "myfonts.hpp"
#include "myimage.hpp"
#include "mylanguage.hpp"

#include <stdlib.h>
///>LCD 显示线程
using namespace device;
using namespace ui;
using namespace systemos;

#define UITHREAD_MENU_MAIN							0
#define UITHREAD_MENU_SETPRESHOW				1
#define UITHREAD_MENU_SETPASSSHOW				2
#define UITHREAD_MENU_SETMAINMENU				3
#define UITHREAD_MENU_PROTECTSET				4
#define UITHREAD_MENU_COMSET						5
#define UITHREAD_MENU_SYSTEMSET					6
#define UITHREAD_MENU_PROTECTSHOW				7
#define UITHREAD_MENU_RECORDSHOW				8

typedef struct
{
	uint8_t ucIndex;
	bool bLongPress;
}KeyEventTPDF;

typedef struct
{
    uint8_t ucMenu;
    void (*FunctionAddr)(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit);
} FormPartitionTPDF;

typedef enum
{
	MYLABLE_SHOWTYPE_TEXT = 0,
	MYLABLE_SHOWTYPE_LIST,
}MyLableShowTypeTPDF;

typedef struct
{
	bool bEnable;
	MyLableShowTypeTPDF tMyLableShowType;
	bool bSetStatus;
	const wchar_t *cHeader;
	const wchar_t *cLeft;
	const wchar_t *cRight;
	const uint32_t *uiList;
	uint8_t ucListLen;
	uint32_t uiLimitUp;
	uint32_t uiLimitDown;
	uint32_t uiStepValue;
	uint32_t *uiValue;
	KeyEventTPDF *tKeyEvent;
	uint32_t uiDataKey;
	uint8_t ucDataLen;
}MyLableTPDF;

typedef struct
{
	MyLableShowTypeTPDF tMyLableShowType;
	uint32_t uiValue;
	const wchar_t *cHeader;
	uint32_t uiStepValue;
	uint32_t uiDataKey;
	uint8_t ucDataLen;
	uint32_t uiLimitUp;
	uint32_t uiLimitDown;
}MyItemTPDF;

static lcd *mLcd = 0;
static drawing *mDrawing = 0;
static drawing::Paint mPaint;
static image *mCoordinateImage;
static image *mLogImage;
static key *mKeys[5];
static stream *mLcdSendStream;
static stream *mLcdRecStream;
static WriteCommandTPDF mWrtCmd;

static bool QueueExchange(void)
{
	WriteStatusTPDF tWriteStatus;
	uint16_t n = 1000;
	
	mLcdSendStream->Public(&mWrtCmd);
	while(!mLcdRecStream->Subscribe(&tWriteStatus))
	{
		Method::Sleep(1);
		n--;
		if(n <= 0)
		{
			break;
		}
	}
	return(tWriteStatus.bResult);
}

static KeyEventTPDF* GetKey(void)
{
	static KeyEventTPDF ret;
	
	uint8_t i;
	for(i = 0; i < 5; i++)
	{
		if(mKeys[i]->Pressed())
		{
			ret.ucIndex = mKeys[i]->KeyIndex;
			ret.bLongPress = false;
			return(&ret);
		}
		if(mKeys[i]->LongPressed())
		{
			ret.ucIndex = mKeys[i]->KeyIndex;
			ret.bLongPress = true;
			return(&ret);
		}
	}
	ret.bLongPress = false;
	ret.ucIndex = 0;
	return(&ret);
}

static void ZeroClear(wchar_t *cString, int len)
{
    uint8_t i;

    for(i = 0; i < len; i++)
    {
        if(cString[i] == L'0')
        {
            cString[i] = L' ';
        }
        else
        {
            break;
        }
    }
}

//
static bool MainCurrentBarGraph(uint8_t num)
{
    static wchar_t wcString[20];
    uint32_t uiCurrent;
    int buf;
    
    switch(num)
    {
        case 0:
		    mDrawing->DrawString(&mPaint, LANG_IA, 0, 0);
            uiCurrent = mTarget.tMeasureData.usIa;
            break;
        case 1:
            mDrawing->DrawString(&mPaint, LANG_IB, 0, 0);
            uiCurrent = mTarget.tMeasureData.usIb;
            break;
        case 2:
            mDrawing->DrawString(&mPaint, LANG_IC, 0, 0);
            uiCurrent = mTarget.tMeasureData.usIc;
            break;
        case 3:
            mDrawing->DrawString(&mPaint, LANG_IN, 0, 0);
            uiCurrent = mTarget.tMeasureData.usIn;
            break;
        case 4:
            mDrawing->DrawString(&mPaint, LANG_IG, 0, 0);
            uiCurrent = mTarget.tMeasureData.usIgnd;
            break;
        default:
            return false;
    }
    memset(wcString, 0, 20);
    buf = mDrawing->font_swprintf(wcString, "%06d A", uiCurrent);
    ZeroClear(wcString, buf);
    if(uiCurrent)
    {
			mDrawing->DrawString(&mPaint, wcString, 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
    }
    else
    {
			mDrawing->DrawString(&mPaint, L"     0 A", 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
    }
    mPaint.PaintStyle = drawing::PAINT_STYLE_FILL;
    mPaint.Invert = false;
    uiCurrent = uiCurrent * 100 / mTarget.tProductInfor.usCurrent;
    if(uiCurrent > 130)///> 画柱状图
    {
			uiCurrent = 130;
    }
    uiCurrent = uiCurrent * 82 / 100;
    mDrawing->DrawRectangle(&mPaint, 11, 16, 11 + uiCurrent, 22);
    mPaint.Invert = true;
    mDrawing->DrawRectangle(&mPaint, 11 + uiCurrent, 16, 120, 22);
    return true;
}

static void MainForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	KeyEventTPDF* tKey;
	static uint8_t ucIndex = 0, ucRefresh = 0;
	static wchar_t wcString[20];
	uint32_t uiCurrent;
	int buf;

	if(bInit)
	{
		mPaint.Invert = true;
		ucIndex = 0;
		ucRefresh = 0;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
		mPaint.Invert = false;
		mDrawing->DrawImage(mCoordinateImage, 0, 16);
	}
	if( usTick> 3000) //ms
	{
		mLcd->vSetTick(0);
		ucIndex++;
		ucIndex %= 6;
		//ucIndex = 5;
		if(ucIndex == 0 || ucIndex == 5)
		{
			mPaint.Invert = true;
			mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
		}
		if(ucIndex == 0)
		{
			mPaint.Invert = false;
			mDrawing->DrawImage(mCoordinateImage, 0, 16);
		}
		ucRefresh = 0;
	}
	if(ucRefresh != 0)
	{
		ucRefresh--;
	}
	else
	{
		ucRefresh = 3;
		switch(ucIndex)
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
				mPaint.Invert = false;
				MainCurrentBarGraph(ucIndex);
				break;
			case 5:
				mPaint.Invert = false;
				mDrawing->DrawString(&mPaint, L"Ia:", 0, 121, 0, drawing::TEXT_ALIGN_LEFT);
				mDrawing->DrawString(&mPaint, L"Ib:", 60, 121, 0, drawing::TEXT_ALIGN_LEFT);
				mDrawing->DrawString(&mPaint, L"Ic:", 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
				mDrawing->DrawString(&mPaint, L"In:", 60, 121, 16, drawing::TEXT_ALIGN_LEFT);
				uiCurrent = mTarget.tMeasureData.usIa;
				memset(wcString, 0, sizeof(wcString));
				buf = mDrawing->font_swprintf(wcString, "%d", uiCurrent);
				ZeroClear(wcString, buf);
				mPaint.Invert = false;
				if(uiCurrent)
				{
					mDrawing->DrawString(&mPaint, wcString, 0, 56, 0, drawing::TEXT_ALIGN_RIGHT);
				}
				else
				{
					mDrawing->DrawString(&mPaint, L"    0", 0, 56, 0, drawing::TEXT_ALIGN_RIGHT);
				}
				uiCurrent = mTarget.tMeasureData.usIb;
				memset(wcString, 0, sizeof(wcString));
				buf = mDrawing->font_swprintf(wcString, "%d", uiCurrent);
				ZeroClear(wcString, buf);
				if(uiCurrent)
				{
					mDrawing->DrawString(&mPaint, wcString, 0, 116, 0, drawing::TEXT_ALIGN_RIGHT);
				}
				else
				{
					mDrawing->DrawString(&mPaint, L"    0", 0, 116, 0, drawing::TEXT_ALIGN_RIGHT);
				}
				uiCurrent = mTarget.tMeasureData.usIc;
				memset(wcString, 0, sizeof(wcString));
				buf = mDrawing->font_swprintf(wcString, "%d", uiCurrent);
				ZeroClear(wcString, buf);
				if(uiCurrent)
				{
					mDrawing->DrawString(&mPaint, wcString, 0, 56, 16, drawing::TEXT_ALIGN_RIGHT);
				}
				else
				{
					mDrawing->DrawString(&mPaint, L"    0", 0, 56, 16, drawing::TEXT_ALIGN_RIGHT);
				}
				uiCurrent = mTarget.tMeasureData.usIn;
				memset(wcString, 0, sizeof(wcString));
				buf = mDrawing->font_swprintf(wcString, "%d", uiCurrent);
				ZeroClear(wcString, buf);
				if(uiCurrent)
				{
					mDrawing->DrawString(&mPaint, wcString, 0, 116, 16, drawing::TEXT_ALIGN_RIGHT);
				}
				else
				{
					mDrawing->DrawString(&mPaint, L"    0", 0, 116, 16, drawing::TEXT_ALIGN_RIGHT);
				}
				break;
		}
	}
	tKey = GetKey();
	switch(tKey->ucIndex)
	{
		case APP_KEY_KEYSET_INDEX:
			*ucMenuIndex = UITHREAD_MENU_SETPRESHOW;
			break;
		case APP_KEY_KEYDOWN_INDEX:
			*ucMenuIndex = UITHREAD_MENU_RECORDSHOW;
			break;
		case APP_KEY_KEYUP_INDEX:
			*ucMenuIndex = UITHREAD_MENU_PROTECTSHOW;
			break;
		default:
			break;
	}
}

static void SetPreShowForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	KeyEventTPDF* tKey;

	if(bInit)
	{
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
	}
	mPaint.Invert = false;
	if(_PRODUCT_ATTRIBUTE & CONFIG_ATTRIBUTE_FLAG_LOGEN)
	{
		if(_PRODUCT_ATTRIBUTE & CONFIG_ATTRIBUTE_FLAG_NOARK)
		{
			mDrawing->DrawString(&mPaint, L"Ex9M", 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
		}
		else
		{
			mDrawing->DrawString(&mPaint, L"NM8", 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
		}
		mDrawing->DrawString(&mPaint, LANG_PARAMSET, 0, 0, 16, drawing::TEXT_ALIGN_CENTER);
	}
	else
	{
		mDrawing->DrawString(&mPaint, LANG_PARAMSET, 0, 0, 8, drawing::TEXT_ALIGN_CENTER);
	}
	if(usTick > 1000)
	{
		*ucMenuIndex = UITHREAD_MENU_SETPASSSHOW;
	}
	tKey = GetKey();
	switch(tKey->ucIndex)
	{
		case APP_KEY_KEYESC_INDEX:
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			break;
		default:
			break;
	}
}

static void SetPassShowForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	const wchar_t InitPswd[8] = {L'0', L'0', L'0', L'0', L'0', L'0', 0, 0};
	static wchar_t wcPwd[8] = {L'0', L'0', L'0', L'0', L'0', L'0', 0, 0};
	static wchar_t wcPwdShow[8] = {L'0', L'0', L'0', L'0', L'0', L'0', 0, 0};
	static uint8_t ucDotIndex = 5;
	static bool bInvert = false;
	KeyEventTPDF* tKey;

	if(bInit)
	{
		memcpy(wcPwd, InitPswd, sizeof(InitPswd));
		ucDotIndex = 5;
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
	}
	mPaint.Invert = false;
	mDrawing->DrawString(&mPaint, LANG_PWDINPUT, 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
	memcpy(wcPwdShow, wcPwd, sizeof(wcPwd));
	if(usTick>500)
	{
		mLcd->vSetTick(0);
		bInvert = !bInvert;
	}
	if(bInvert)
	{
		wcPwdShow[ucDotIndex] = L' ';
	}
	mDrawing->DrawString(&mPaint, wcPwdShow, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
	tKey = GetKey();
	switch(tKey->ucIndex)
	{
		case APP_KEY_KEYUP_INDEX:
			wcPwd[ucDotIndex]++;
			if(wcPwd[ucDotIndex] > L'9')
			{
				wcPwd[ucDotIndex] = 0x30;
			}
			break;
		case APP_KEY_KEYDOWN_INDEX:
			wcPwd[ucDotIndex]--;
			if(wcPwd[ucDotIndex] < L'0')
			{
				wcPwd[ucDotIndex] = L'9';
			}
			break;
		case APP_KEY_KEYSET_INDEX:
			mDrawing->font_swprintf(wcPwdShow,(char*)(mTarget.tSecurityConfig.ucPassword));
			if(memcmp(wcPwd, wcPwdShow, 6*sizeof(wchar_t)) == 0)        ///<liwei
			{
				*ucMenuIndex = UITHREAD_MENU_SETMAINMENU;
			}
			else
			{
				*ucMenuIndex = UITHREAD_MENU_MAIN;
			}
			break;
		case APP_KEY_KEYESC_INDEX:
			ucDotIndex--;
			if(ucDotIndex == 0xff)
			{
				ucDotIndex = 5;
			}
			break;
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			break;
		default:
			break;
	}
}

static void SetMainMenuForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	static uint8_t ucMenu = 0;
	KeyEventTPDF* tKey;

	if(bInit)
	{
		ucMenu = 0;
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
		mPaint.Invert = false;
		mDrawing->DrawString(&mPaint, LANG_MAINMENU, 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
	}
	mPaint.Invert = false;
	switch(ucMenu)
	{
		case 0:
			mDrawing->DrawString(&mPaint, LANG_PRTMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			break;
		case 1:
			mDrawing->DrawString(&mPaint, LANG_COMMMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			break;
		case 2:
			mDrawing->DrawString(&mPaint, LANG_SYSTEMMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			break;
	}
	tKey = GetKey();
	switch(tKey->ucIndex)
	{
		case APP_KEY_KEYUP_INDEX:
			ucMenu--;
			if(ucMenu == 0xff)
			{
				ucMenu = 2;
			}
			break;
		case APP_KEY_KEYDOWN_INDEX:
			ucMenu++;
			if(ucMenu == 3)
			{
				ucMenu = 0;
			}
			break;
		case APP_KEY_KEYSET_INDEX:
			switch(ucMenu)
			{
				case 0:
					*ucMenuIndex = UITHREAD_MENU_PROTECTSET;
					break;
				case 1:
					*ucMenuIndex = UITHREAD_MENU_COMSET;
					break;
				case 2:
					*ucMenuIndex = UITHREAD_MENU_SYSTEMSET;
					break;
			}
			break;
		case APP_KEY_KEYESC_INDEX:
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			break;
		default:
			break;
	}
}

static void SetLableShow(MyLableTPDF *tMyLable, bool bInited, bool bSecondLineClear)
{
	static bool bSetStatus = false;
	wchar_t wcSetStatus[3] = {0xfff0, 0};
	uint8_t ucLeftLen, ucRightLen;
	uint8_t i;

	if(tMyLable->tMyLableShowType == MYLABLE_SHOWTYPE_LIST)
	{
		for(i = 0; i < tMyLable->ucListLen; i++)
		{
			if(tMyLable->uiList[i] == *tMyLable->uiValue)
			{
				break;
			}
		}
	}
	tMyLable->tKeyEvent = GetKey();
	if(bInited)
	{
		bSetStatus = false;
		mPaint.Invert = true;
		mDrawing->Clear();
		mPaint.Invert = false;
		mDrawing->DrawString(&mPaint, tMyLable->cHeader, 0, 121, 0, drawing::TEXT_ALIGN_LEFT);
	}
	if(bSecondLineClear)
	{
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 16, 120, 31);
		mPaint.Invert = false;
	}
	if(bSetStatus == true)
	{
		mDrawing->DrawString(&mPaint, wcSetStatus, 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
	}
	else
	{
		mDrawing->DrawString(&mPaint, L"    ", 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
	}
	mPaint.Invert = false;
	ucLeftLen = mDrawing->MeasureString(tMyLable->cLeft);
	ucRightLen = mDrawing->MeasureString(tMyLable->cRight);
	mDrawing->DrawString(&mPaint, tMyLable->cLeft, 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
	mDrawing->DrawString(&mPaint, tMyLable->cRight, 0, 121, 16, drawing::TEXT_ALIGN_RIGHT);
	if((ucLeftLen + ucRightLen) < 120)
	{
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, ucLeftLen, 16, 120 - ucRightLen, 31);
		mPaint.Invert = false;
	}
	switch(tMyLable->tKeyEvent->ucIndex)
	{
		case APP_KEY_KEYUP_INDEX:
			if(!tMyLable->bEnable)
			{
				return;
			}
			if(!bSetStatus)
			{
				return;
			}
			if(tMyLable->tMyLableShowType == MYLABLE_SHOWTYPE_LIST)
			{
				i++;
				i %= tMyLable->ucListLen;
				*tMyLable->uiValue = tMyLable->uiList[i];
				break;
			}
			*tMyLable->uiValue += tMyLable->uiStepValue;
			if(*tMyLable->uiValue > tMyLable->uiLimitUp)
			{
				*tMyLable->uiValue = tMyLable->uiLimitDown;
			}
			break;
		case APP_KEY_KEYDOWN_INDEX:
			if(!tMyLable->bEnable)
			{
				return;
			}
			if(!bSetStatus)
			{
				return;
			}
			if(tMyLable->tMyLableShowType == MYLABLE_SHOWTYPE_LIST)
			{
				i--;
				if(i == 0xff)
				{
					i = tMyLable->ucListLen - 1;
				}
				*tMyLable->uiValue = tMyLable->uiList[i];
				break;
			}
			if(tMyLable->tMyLableShowType == MYLABLE_SHOWTYPE_LIST)
			{
				break;
			}
			*tMyLable->uiValue -= tMyLable->uiStepValue;
			if(*tMyLable->uiValue > tMyLable->uiLimitUp || *tMyLable->uiValue < tMyLable->uiLimitDown)
			{
				*tMyLable->uiValue = tMyLable->uiLimitUp;
			}
			break;
		case APP_KEY_KEYSET_INDEX:
			if(!tMyLable->bEnable)
			{
				return;
			}
			if(!bSetStatus)
			{
				tMyLable->bSetStatus = true;
				bSetStatus = true;
			}
			else
			{
				tMyLable->bSetStatus = false;
				bSetStatus = false;
				return;
			}
			break;
		default:
			return;
	}
	tMyLable->tKeyEvent->ucIndex = 0;
}

static void SetProtectForm_Motor(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	const uint32_t uiLongDelayTimeTbl[] = {5, 10, 20, 30};

	static uint8_t ucMenu, ucMenuBack;
	static wchar_t wcString[16], wcString2[16];
	static MyLableTPDF tMyLable;
	static uint32_t i, k;
	static float j;
	volatile MyItemTPDF tMyItem[] = 
	{
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usLongDelayEnable, LANG_LGDL_EN_DES, 1, 
		 LcdCustomConfig_LongDelayEnable_Attribute.uiKey,LcdCustomConfig_LongDelayEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usLongDelayCurrent, LANG_LGDL_C_DES, 1, 
		 LcdCustomConfig_LongDelayCurrent_Attribute.uiKey,LcdCustomConfig_LongDelayCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_LIST, mTarget.tLcdCustomConfig.usLongDelayTime, LANG_LGDL_T_DES, 1, 
		 LcdCustomConfig_Motor_LongDelayTime_Attribute.uiKey,LcdCustomConfig_Motor_LongDelayTime_Attribute.ucLen,
		 0,
		 0},///> 电机长延时列表显示
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortDelayEnable, LANG_STDL_EN_DES, 1, 
		 LcdCustomConfig_ShortDelayEnable_Attribute.uiKey,LcdCustomConfig_ShortDelayEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortDelayCurrent, LANG_STDL_C_DES, 1, 
		 LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.uiKey,LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.ucLen,
		 mTarget.tLcdCustomConfig.usLongDelayEnable? (uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper / 100:
		                                             (uint32_t)(uint32_t)(uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 mTarget.tLcdCustomConfig.usLongDelayEnable? (uint32_t)(uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower / 100:
		                                             (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usBlockEnable, LANG_BLK_EN_DES, 1, 
		 LcdCustomConfig_BlockEnable_Attribute.uiKey, LcdCustomConfig_BlockEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usBlockCurrent, LANG_BLK_C_DES, 1, 
		 LcdCustomConfig_BlockCurrent_Attribute.uiKey, LcdCustomConfig_BlockCurrent_Attribute.ucLen,
		 mTarget.tLcdCustomConfig.usLongDelayEnable? (uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iUpper / 100:
																								 (uint32_t)(uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 mTarget.tLcdCustomConfig.usLongDelayEnable? (uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iLower / 100:
																								 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usBlockTime, LANG_BLK_T_DES, 1, 
		 LcdCustomConfig_BlockTime_Attribute.uiKey, LcdCustomConfig_BlockTime_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_BlockTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_BlockTime_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usUnbalanceEnable, LANG_UNB_EN_DES, 1, 
		 LcdCustomConfig_UnbalanceEnable_Attribute.uiKey, LcdCustomConfig_UnbalanceEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usUnbalanceRated, LANG_UNB_C_DES, 10, 
		 LcdCustomConfig_UnbalanceRated_Attribute.uiKey, LcdCustomConfig_UnbalanceRated_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_UnbalanceRated_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_UnbalanceRated_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usUnbalanceTime, LANG_UNB_T_DES, 2, 
		 LcdCustomConfig_UnbalanceTime_Attribute.uiKey, LcdCustomConfig_UnbalanceTime_Attribute.ucLen,
		 (uint32_t)(uint32_t)LcdCustomConfig_UnbalanceTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_UnbalanceTime_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthEnable, LANG_EARTH_EN_DES, 1, 
		 LcdCustomConfig_EarthEnable_Attribute.uiKey, LcdCustomConfig_EarthEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthCurrent, LANG_EARTH_C_DES, 1, 
		 LcdCustomConfig_EarthCurrent_Attribute.uiKey, LcdCustomConfig_EarthCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthTime, LANG_EARTH_T_DES, 100, 
		 LcdCustomConfig_EarthTime_Attribute.uiKey, LcdCustomConfig_EarthTime_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iLower},
	};

	if(bInit)
	{
		ucMenu = 0;
		ucMenuBack = 1;
		tMyLable.bEnable = true;
		tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
	}
	memset(wcString, 0, sizeof(wcString));
	memset(wcString2, 0, sizeof(wcString2));
	if(ucMenuBack != ucMenu)
	{
		i = tMyItem[ucMenu].uiValue;
		tMyLable.tMyLableShowType = tMyItem[ucMenu].tMyLableShowType;
		tMyLable.uiValue = &i;
		tMyLable.uiStepValue = tMyItem[ucMenu].uiStepValue;
		tMyLable.cHeader = tMyItem[ucMenu].cHeader;
		tMyLable.uiDataKey = tMyItem[ucMenu].uiDataKey;
		tMyLable.ucDataLen = tMyItem[ucMenu].ucDataLen;
		tMyLable.uiLimitUp = tMyItem[ucMenu].uiLimitUp;
		tMyLable.uiLimitDown = tMyItem[ucMenu].uiLimitDown;
	}
	switch(ucMenu)
	{
		case 0:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 1:
			k = (i * 100) / mTarget.tProductInfor.usCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Ir(%.2fIn) ", j);
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 2:
			if(ucMenuBack != ucMenu)
			{
				tMyLable.uiList = uiLongDelayTimeTbl;
				tMyLable.ucListLen = sizeof(uiLongDelayTimeTbl) >> 2;
			}
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = L"Class:";
			tMyLable.cRight = wcString2;
			break;
		case 3:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 4:
			if(mTarget.tLcdCustomConfig.usLongDelayEnable)
			{
				k = (i * 100) / mTarget.tLcdCustomConfig.usLongDelayCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Isd:%.1fxIr ", j);
			}
			else
			{
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Isd:%.1fxIn ", j);
			}
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 5:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 6:
			k = (i * 100) / mTarget.tLcdCustomConfig.usLongDelayCurrent;
			j = ((float)k) / 100.0f;
			if(mTarget.tLcdCustomConfig.usLongDelayEnable)
			{
				mDrawing->font_swprintf(wcString, "Ijam(%.1fIr) ", j);
			}
			else
			{
				mDrawing->font_swprintf(wcString, "Ijam(%.1fIn) ", j);
			}
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 7:
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = L"Tjam(S):";
			tMyLable.cRight = wcString2;
			break;
		case 8:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 9:
			j = ((float)i) / 100.0f;
			mDrawing->font_swprintf(wcString2, "%.1f", j);
			tMyLable.cLeft = L"Iu:";
			tMyLable.cRight = wcString2;
			break;
		case 10:
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = L"Tu(s):";
			tMyLable.cRight = wcString2;
			break;
		case 11:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 12:
			k = (i * 100) / mTarget.tProductInfor.usCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Ig(%.2fIn) ", j);
			mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 13:
			j = ((float)i) / 1000.0f;
			mDrawing->font_swprintf(wcString2, "%.1f", j);
			tMyLable.cLeft = L"Tg(S):";
			tMyLable.cRight = wcString2;
			break;
	}
	if(ucMenuBack != ucMenu)
	{
		ucMenuBack = ucMenu;
		SetLableShow(&tMyLable, true, false);
	}
	else
	{
		SetLableShow(&tMyLable, false, false);
	}
	if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
	{
		mWrtCmd.tType = WRITE_COMMAND_CUSTOMCONFIG;
		mWrtCmd.tWriteData[0].usLen = 2;
		mWrtCmd.tWriteData[0].ucData[0] = i & 0x000000ff;
		mWrtCmd.tWriteData[0].ucData[1] = (i >> 8) & 0x000000ff;
		mWrtCmd.tWriteData[0].uiKey = tMyLable.uiDataKey;
		QueueExchange();
		tMyLable.tKeyEvent->ucIndex = 0;
	}
	switch(tMyLable.tKeyEvent->ucIndex)
	{
		case APP_KEY_KEYESC_INDEX:
			*ucMenuIndex = UITHREAD_MENU_SETMAINMENU;
			return;
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			return;
		case APP_KEY_KEYDOWN_INDEX:
			if(ucMenu == 0 && !mTarget.tLcdCustomConfig.usLongDelayEnable)
			{
				ucMenu = 3;
			}
			else if(ucMenu == 3 && !mTarget.tLcdCustomConfig.usShortDelayEnable)
			{
				ucMenu = 5;
			}
			else if(ucMenu == 5 && !mTarget.tLcdCustomConfig.usBlockEnable)
			{
				ucMenu = 8;
			}
			else if(ucMenu == 8 && !mTarget.tLcdCustomConfig.usUnbalanceEnable)
			{
				ucMenu = 11;
			}
			else if(ucMenu == 11 && !mTarget.tLcdCustomConfig.usEarthEnable)
			{
				ucMenu = 0;
			}
			else
			{
				ucMenu++;
				ucMenu %= 14;
			}
			break;
		case APP_KEY_KEYUP_INDEX:
			if(ucMenu == 0 && !mTarget.tLcdCustomConfig.usEarthEnable)
			{
				ucMenu = 11;
			}
			else if(ucMenu == 3 && !mTarget.tLcdCustomConfig.usLongDelayEnable)
			{
				ucMenu = 0;
			}
			else if(ucMenu == 5 && !mTarget.tLcdCustomConfig.usShortDelayEnable)
			{
				ucMenu = 3;
			}
			else if(ucMenu == 8 && !mTarget.tLcdCustomConfig.usBlockEnable)
			{
				ucMenu = 5;
			}
			else if(ucMenu == 11 && !mTarget.tLcdCustomConfig.usUnbalanceEnable)
			{
				ucMenu = 8;
			}
			else
			{
				ucMenu--;
				if(ucMenu == 0xff)
				{
					ucMenu = 13;
				}
			}
			break;
		default:
			break;
	}
}

static void SetProtectForm_Distrib(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	static uint8_t ucMenu, ucMenuBack;
	static wchar_t wcString[16], wcString2[16];
	static MyLableTPDF tMyLable;
	static uint32_t i, k;
	static float j;
	int buf;

	volatile MyItemTPDF tMyItem[] = 
	{
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usLongDelayCurrent, LANG_LGDL_C_DES, 1, 
		 LcdCustomConfig_LongDelayCurrent_Attribute.uiKey, LcdCustomConfig_LongDelayCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usLongDelayTime, LANG_LGDL_T_DES, 1, 
		 LcdCustomConfig_Distrib_LongDelayTime_Attribute.uiKey, LcdCustomConfig_Distrib_LongDelayTime_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_Distrib_LongDelayTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_Distrib_LongDelayTime_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortDelayEnable, LANG_STDL_EN_DES, 1, 
		 LcdCustomConfig_ShortDelayEnable_Attribute.uiKey, LcdCustomConfig_ShortDelayEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortDelayCurrent, LANG_STDL_C_DES, 1, 
		 LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.uiKey, LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tLcdCustomConfig.usLongDelayCurrent * LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortDelayTime, LANG_STDL_T_DES, 100, 
		 LcdCustomConfig_Distrib_ShortDelayTime_Attribute.uiKey, LcdCustomConfig_Distrib_ShortDelayTime_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_Distrib_ShortDelayTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_Distrib_ShortDelayTime_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortCircuitEnable, LANG_STCC_EN_DES, 1, 
		 LcdCustomConfig_ShortCircuitEnable_Attribute.uiKey, LcdCustomConfig_ShortCircuitEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usShortCircuitCurrent, LANG_STCC_C_DES, 1, 
		 LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.uiKey, LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usNEnable, LANG_NC_EN_DES, 1, 
		 LcdCustomConfig_NEnable_Attribute.uiKey, LcdCustomConfig_NEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usNCurrent, LANG_NC_C_DES, 5, 
		 LcdCustomConfig_NCurrent_Attribute.uiKey, LcdCustomConfig_NCurrent_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_NCurrent_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_NCurrent_Attribute.tVariableDataLimit.iLower},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthEnable, LANG_EARTH_EN_DES, 1, 
		 LcdCustomConfig_EarthEnable_Attribute.uiKey, LcdCustomConfig_EarthEnable_Attribute.ucLen,
		 1,
		 0},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthCurrent, LANG_EARTH_C_DES, 1, 
		 LcdCustomConfig_EarthCurrent_Attribute.uiKey, LcdCustomConfig_EarthCurrent_Attribute.ucLen,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iUpper / 100,
		 (uint32_t)mTarget.tProductInfor.usCurrent * LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iLower / 100},
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tLcdCustomConfig.usEarthTime, LANG_EARTH_T_DES, 100, 
		 LcdCustomConfig_EarthTime_Attribute.uiKey, LcdCustomConfig_EarthTime_Attribute.ucLen,
		 (uint32_t)LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iUpper,
		 (uint32_t)LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iLower},
	};
	
	if(bInit)
	{
		ucMenu = 0;
		ucMenuBack = 1;
		tMyLable.bEnable = true;
		tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
	}
	memset(wcString, 0, sizeof(wcString));
	memset(wcString2, 0, sizeof(wcString2));
	if(ucMenuBack != ucMenu)
	{
		i = tMyItem[ucMenu].uiValue;
		tMyLable.tMyLableShowType = tMyItem[ucMenu].tMyLableShowType;
		tMyLable.uiValue = &i;
		tMyLable.uiStepValue = tMyItem[ucMenu].uiStepValue;
		tMyLable.cHeader = tMyItem[ucMenu].cHeader;
		tMyLable.uiDataKey = tMyItem[ucMenu].uiDataKey;
		tMyLable.ucDataLen = tMyItem[ucMenu].ucDataLen;
		tMyLable.uiLimitUp = tMyItem[ucMenu].uiLimitUp;
		tMyLable.uiLimitDown = tMyItem[ucMenu].uiLimitDown;
	}
	switch(ucMenu)
	{
		case 0:
			k = (i * 100) / mTarget.tProductInfor.usCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Ir(%.2fIn) ", j);
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 1:
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = L"Tr(6Ir S)";
			tMyLable.cRight = wcString2;
			break;
		case 2:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 3:
			k = (i * 100) / mTarget.tLcdCustomConfig.usLongDelayCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Isd:%.1fxIr ", j);
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 4:
			j = ((float)i) / 1000.0f;
			mDrawing->font_swprintf(wcString2, "%.1f", j);
			tMyLable.cLeft = L"Tsd(S): ";
			tMyLable.cRight = wcString2;
			break;
		case 5:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 6:
			k = (i * 100) / mTarget.tProductInfor.usCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Ii(%.1fIn) ", j);
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 7:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 8:
			j = ((float)i) / 10.0f;
			mDrawing->font_swprintf(wcString2, "%.1fxIn", j);
			tMyLable.cLeft = L"In(xIn)";
			tMyLable.cRight = wcString2;
			break;
		case 9:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 10:
			k = (i * 100) / mTarget.tProductInfor.usCurrent;
			j = ((float)k) / 100.0f;
			mDrawing->font_swprintf(wcString, "Ig(%.2fIn) ", j);
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = wcString;
			tMyLable.cRight = wcString2;
			break;
		case 11:
			j = ((float)i) / 1000.0f;
			mDrawing->font_swprintf(wcString2, "%.1f", j);
			tMyLable.cLeft = L"Tg(S):";
			tMyLable.cRight = wcString2;
			break;
	}
	if(ucMenuBack != ucMenu)
	{
		ucMenuBack = ucMenu;
		SetLableShow(&tMyLable, true, false);
	}
	else
	{
		SetLableShow(&tMyLable, false, false);
	}
	if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
	{
		mWrtCmd.tType = WRITE_COMMAND_CUSTOMCONFIG;
		mWrtCmd.tWriteData[0].usLen = 2;
		mWrtCmd.tWriteData[0].ucData[0] = i & 0x000000ff;
		mWrtCmd.tWriteData[0].ucData[1] = (i >> 8) & 0x000000ff;
		mWrtCmd.tWriteData[0].uiKey = tMyLable.uiDataKey;
		QueueExchange();
		tMyLable.tKeyEvent->ucIndex = 0;
	}
	switch(tMyLable.tKeyEvent->ucIndex)
	{
		case APP_KEY_KEYESC_INDEX:
			*ucMenuIndex = UITHREAD_MENU_SETMAINMENU;
			return;
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			return;
		case APP_KEY_KEYDOWN_INDEX:
			if(ucMenu == 2 && !mTarget.tLcdCustomConfig.usShortDelayEnable)
			{
				ucMenu = 5;
			}
			else if(ucMenu == 5 && !mTarget.tLcdCustomConfig.usShortCircuitEnable)
			{
				if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_4WIRE)
				{
					ucMenu = 7;
				}
				else
				{
					ucMenu = 9;
				}
			}
			else if(ucMenu == 6)
			{
				if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_4WIRE)
				{
					ucMenu = 7;
				}
				else
				{
					ucMenu = 9;
				}
			}
			else if(ucMenu == 7 && !mTarget.tLcdCustomConfig.usNEnable)
			{
				ucMenu = 9;
			}
			else if(ucMenu == 9 && !mTarget.tLcdCustomConfig.usEarthEnable)
			{
				ucMenu = 0;
			}
			else
			{
				ucMenu++;
				ucMenu %= 12;
			}
			break;
		case APP_KEY_KEYUP_INDEX:
			if(ucMenu == 9)
			{
				if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_4WIRE)
				{
					if(mTarget.tLcdCustomConfig.usNEnable)
					{
						ucMenu = 8;
					}
					else
					{
						ucMenu = 7;
					}
				}
				else
				{
					if(mTarget.tLcdCustomConfig.usShortCircuitEnable)
					{
						ucMenu = 6;
					}
					else
					{
						ucMenu = 5;
					}
				}
			}
			else if(ucMenu == 7 && !mTarget.tLcdCustomConfig.usShortCircuitEnable)
			{
				ucMenu = 5;
			}
			else if(ucMenu == 5 && !mTarget.tLcdCustomConfig.usShortDelayEnable)
			{
				ucMenu = 2;
			}
			else if(ucMenu == 0 && !mTarget.tLcdCustomConfig.usEarthEnable)
			{
				ucMenu = 9;
			}
			else
			{
				ucMenu--;
				if(ucMenu == 0xff)
				{
					ucMenu = 11;
				}
			}
			break;
		default:
			break;
	}
}

static void SetProtectForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_MOTOR)
	{
		SetProtectForm_Motor(ucMenuIndex, usTick, bInit);
	}
	else
	{
		SetProtectForm_Distrib(ucMenuIndex, usTick, bInit);
	}
}
///通过建表的方式MyItemTPDF，建立没一页的数据项
static void SetComForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	static uint8_t ucMenu, ucMenuBack;
	static wchar_t wcString[16], wcString2[16];
	static MyLableTPDF tMyLable;
	static uint32_t i;
	int buf;
	volatile MyItemTPDF tMyItem[] = 
	{
		{MYLABLE_SHOWTYPE_TEXT, mTarget.tComConfig.usAddr, LANG_ADDRSET_DES, 1,
		 ComConfig_Addr_Attribute.uiKey, ComConfig_Addr_Attribute.ucLen,247, 1},

		{MYLABLE_SHOWTYPE_TEXT, mTarget.tComConfig.usBaud, LANG_BAUDSET_DES, 9600,
		 ComConfig_Baud_Attribute.uiKey, ComConfig_Baud_Attribute.ucLen, 19200, 9600},

		{MYLABLE_SHOWTYPE_TEXT, mTarget.tSecurityConfig.usNetChangeEnable, LANG_NETCGSET_DES, 1, 
		 Security_NetChangeEnable_Attribute.uiKey, Security_NetChangeEnable_Attribute.ucLen, 1, 0},

		{MYLABLE_SHOWTYPE_TEXT, mTarget.tSecurityConfig.usNetControlEnable, LANG_NETCTSET_DES, 1, 
		 Security_NetControlEnable_Attribute.uiKey, Security_NetControlEnable_Attribute.ucLen, 1, 0}
	};
	
	if(bInit)
	{
		ucMenu = 0;
		ucMenuBack = 1;
		tMyLable.bEnable = true;
	}
	memset(wcString, 0, sizeof(wcString));
	memset(wcString2, 0, sizeof(wcString2));
	if(ucMenuBack != ucMenu)
	{
		i = tMyItem[ucMenu].uiValue;
		tMyLable.tMyLableShowType = tMyItem[ucMenu].tMyLableShowType;
		tMyLable.uiValue = &i;
		tMyLable.uiStepValue = tMyItem[ucMenu].uiStepValue;
		tMyLable.cHeader = tMyItem[ucMenu].cHeader;
		tMyLable.uiDataKey = tMyItem[ucMenu].uiDataKey;
		tMyLable.ucDataLen = tMyItem[ucMenu].ucDataLen;
		tMyLable.uiLimitUp = tMyItem[ucMenu].uiLimitUp;
		tMyLable.uiLimitDown = tMyItem[ucMenu].uiLimitDown;
	}
	switch(ucMenu)
	{
		case 0:
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = LANG_ADDRSET;
			tMyLable.cRight = wcString2;
			break;
		case 1:
			buf = mDrawing->font_swprintf(wcString2, "%d", i);
			tMyLable.cLeft = LANG_BAUDSET;
			tMyLable.cRight = wcString2;
			break;
		case 2:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
		case 3:
			tMyLable.cLeft = LANG_STATE;
			tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
			break;
	}
	if(ucMenuBack != ucMenu)
	{
		ucMenuBack = ucMenu;
		SetLableShow(&tMyLable, true, false);
	}
	else
	{
		SetLableShow(&tMyLable, false, false);
	}
	if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
	{
		if(ucMenu == 0 || ucMenu == 1)
		{
			mWrtCmd.tType = WRITE_COMMAND_COMCONFIG;
		}
		else if(ucMenu == 2 || ucMenu == 3)
		{
			mWrtCmd.tType = WRITE_COMMAND_SECURITY;
		}
		mWrtCmd.tWriteData[0].usLen = 2;
		mWrtCmd.tWriteData[0].ucData[0] = i & 0x000000ff;
		mWrtCmd.tWriteData[0].ucData[1] = (i >> 8) & 0x000000ff;
		mWrtCmd.tWriteData[0].uiKey = tMyLable.uiDataKey;
		QueueExchange();
		tMyLable.tKeyEvent->ucIndex = 0;
	}
	switch(tMyLable.tKeyEvent->ucIndex)
	{
		case APP_KEY_KEYESC_INDEX:
			*ucMenuIndex = UITHREAD_MENU_SETMAINMENU;
			return;
		case APP_KEY_KEYPCIO_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			return;
		case APP_KEY_KEYDOWN_INDEX:
			ucMenu++;
			ucMenu %= 4;
			break;
		case APP_KEY_KEYUP_INDEX:
			ucMenu--;
			if(ucMenu == 0xff)
			{
				ucMenu = 3;
			}
			break;
		default:
			break;
	}
}

static void SetSystemForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	static uint8_t ucMenu = 0;                      ///<主菜单:1.界面语言,2.口令保护,3.软件版本
	static bool bSubMenu = false;                   ///<是否在子菜单状态
	static bool bSubMenuBack = false;           		///<是否在子菜单状态备份
	static uint8_t ucSubMenu = 0;                   ///<口令保护设置下的子菜单
	static bool bSubMenuChange = false;    			 		///<口令保护设置下的子菜单是否发生切换
	static bool bSubMenuEdited = false;     				///<设置新口令是否在编辑状态
	static uint8_t ucPassWordIndex = 0;     				///<口令索引号
	static MyLableTPDF tMyLable;                    ///<文本框
	static uint32_t i = 0;
	static wchar_t wcString[20], wcString2[20], wcString2Back[20];
	int buf;
	uint8_t j;
	KeyEventTPDF* tKey;                             ///<按键事件

	if(bInit)           ///<初始化
	{
		ucMenu = 0;
		bSubMenu = false;
		bSubMenuBack = false;
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
		mPaint.Invert = false;
		mDrawing->DrawString(&mPaint, LANG_SYSTEMSUBMENU, 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
	}
	if((!bSubMenu) && bSubMenuBack) ///<子菜单切换到主菜单
	{
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
		mPaint.Invert = false;
		mDrawing->DrawString(&mPaint, LANG_SYSTEMSUBMENU, 0, 121, 0, drawing::TEXT_ALIGN_CENTER);
	}
	mPaint.Invert = false;
	switch(ucMenu)
	{
		case 0:
			///<子菜单语言选择
			if(bSubMenu)
			{
			if(bSubMenu != bSubMenuBack)    ///<界面初始化
			{
			i = mTarget.tSecurityConfig.usLanguage;
			tMyLable.uiValue = &i;
			tMyLable.bEnable = true;
			tMyLable.uiLimitUp = 1;
			tMyLable.uiLimitDown = 0;
			tMyLable.uiStepValue = 1;
			tMyLable.cHeader = LANG_LANGUAGE_DES;
			tMyLable.cLeft = LANG_LANGUAGE;
			tMyLable.cRight = i ? L"English" : L"  简体中文";
			SetLableShow(&tMyLable, true, false);
			}
			else
			{
			tMyLable.cRight = i ? L"English" : L"  简体中文";
			SetLableShow(&tMyLable, false, false);

			if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
			{

			mWrtCmd.tType = WRITE_COMMAND_SECURITY;
			mWrtCmd.tWriteData[0].usLen = 2;
			mWrtCmd.tWriteData[0].ucData[0] = i & 0x000000ff;
			mWrtCmd.tWriteData[0].ucData[1] = (i >> 8) & 0x000000ff;
			mWrtCmd.tWriteData[0].uiKey = Security_Language_Attribute.uiKey;
			QueueExchange();
			tMyLable.tKeyEvent->ucIndex =0;
			}
			}
			}
			///<主菜单界面语言
			else
			{
			mDrawing->DrawString(&mPaint, LANG_LANGUAGEMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			}
			break;
		case 1:
			///<子菜单口令保护设置
			if(bSubMenu)
			{
				if(bSubMenu != bSubMenuBack)
				{
					ucSubMenu = 0;
				}
				if(!ucSubMenu)  ///<口令保护设置
				{
					if(bSubMenu != bSubMenuBack || bSubMenuChange)  ///<初始化
					{
						bSubMenuChange = false;
						i = mTarget.tSecurityConfig.usPasswordEnable;
						tMyLable.uiValue = &i;
						tMyLable.bEnable = true;
						tMyLable.uiLimitUp = 1;
						tMyLable.uiLimitDown = 0;
						tMyLable.uiStepValue = 1;
						tMyLable.cHeader = LANG_PWD_DES;
						tMyLable.cLeft = LANG_STATE;
						tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
						SetLableShow(&tMyLable, true, false);
					}
					else
					{
						tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
						SetLableShow(&tMyLable, false, false);
						if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
						{
							mWrtCmd.tType = WRITE_COMMAND_SECURITY;
							mWrtCmd.tWriteData[0].usLen = 2;
							mWrtCmd.tWriteData[0].ucData[0] = i & 0x000000ff;
							mWrtCmd.tWriteData[0].ucData[1] = (i >> 8) & 0x000000ff;
							mWrtCmd.tWriteData[0].uiKey = Security_PasswordEnable_Attribute.uiKey;
							QueueExchange();
							tMyLable.tKeyEvent->ucIndex =0;
						}
					}
				}
				else        ///<设置新口令
				{
					if(bSubMenu != bSubMenuBack || bSubMenuChange)  ///<初始化
					{
						bSubMenuChange = false;
						bSubMenuEdited = false;
						tMyLable.bSetStatus = false;
						tMyLable.uiValue = &i;
						tMyLable.bEnable = true;
						tMyLable.uiLimitUp = '9';
						tMyLable.uiLimitDown = '0';
						tMyLable.uiStepValue = 1;
						tMyLable.cHeader = LANG_PWDNEW_DES1;
						ucPassWordIndex = 0;
						memset(wcString, 0, sizeof(wcString));
						memcpy(wcString, LANG_ESC_ENT, std::strlen((const char*)LANG_ESC_ENT));
						if(mTarget.tSecurityConfig.usLanguage)
						{
							memcpy(wcString, L" :Esc       :Ent ", 36);
						}
						else
						{
							memcpy(wcString, L" :跳过      :修改", 36);
						}
						wcString[9] = 0xfff1;
						wcString[0] = 0xfff0;
						tMyLable.cLeft = wcString;
						tMyLable.cRight = L"";
						SetLableShow(&tMyLable, true, false);
					}
					else
					{
						if(tMyLable.bSetStatus)         ///<设置状态下
						{
							tMyLable.cHeader = LANG_PWDNEW_DES2;
							if(!bSubMenuEdited) ///<初始化
							{
								bSubMenuEdited = true;
								for(uint8_t z = 0; z < 20; z++)
								{
									wcString2[z] = ' ';
								}
								for(uint8_t z = 5; z < 11; z++)
								{
									wcString2[z] = '0';
								}
								ucPassWordIndex = 0;
								mLcd->vSetTick(0);
								i = wcString2[10];
								tMyLable.cLeft = wcString2;
								SetLableShow(&tMyLable, false, true);
							}
							else
							{
								tMyLable.cLeft = wcString2Back;
								wcString2[10 - ucPassWordIndex] = i;
								memcpy(wcString2Back, wcString2, sizeof(wcString2));
								///<闪烁显示口令
								if(usTick < 300)
								{
									wcString2Back[10 - ucPassWordIndex] = ' ';
								}
								else if(usTick >= 600)
								{
									mLcd->vSetTick(0);
								}
								SetLableShow(&tMyLable, false, false);
								if(tMyLable.tKeyEvent->ucIndex == APP_KEY_KEYSET_INDEX)
								{
									mWrtCmd.tType = WRITE_COMMAND_SECURITY;
									mWrtCmd.tWriteData[0].usLen = 6;
									for(uint8_t z = 0; z < 6; z++)
									{
										mWrtCmd.tWriteData[0].ucData[z] = wcString2[z + 5];
									}
									mWrtCmd.tWriteData[0].uiKey = Security_Password_Attribute.uiKey;
									QueueExchange();
									tMyLable.tKeyEvent->ucIndex =0;
								}
							}
							switch(tMyLable.tKeyEvent->ucIndex)
							{
								case APP_KEY_KEYESC_INDEX:
									ucPassWordIndex++;
									ucPassWordIndex %= 6;
									i = wcString2[10 - ucPassWordIndex];
									tMyLable.tKeyEvent->ucIndex = 0;
									break;
								default:
									break;
							}
						}
						else            ///<非设置状态下
						{
							tMyLable.cHeader = LANG_PWDNEW_DES1;
							tMyLable.cLeft = wcString;

							if(bSubMenuEdited)  ///<初始化
							{
								bSubMenuEdited = false;
								SetLableShow(&tMyLable, false, true);
							}
							else
							{
								SetLableShow(&tMyLable, false, false);
							}
						}
					}
				}
				switch(tMyLable.tKeyEvent->ucIndex)
				{
					case APP_KEY_KEYUP_INDEX:
					case APP_KEY_KEYDOWN_INDEX:
						if(ucSubMenu)
						{
							ucSubMenu = 0;
						}
						else
						{
						ucSubMenu = 1;
						}
						bSubMenuChange = true;
						break;
					default:
						break;
				}
			}
			///<主菜单口令保护
			else
			{
				mDrawing->DrawString(&mPaint, LANG_PWDMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			}
			break;
		case 2:
			///<子菜单软件版本
			if(bSubMenu)
			{
				if(bSubMenu != bSubMenuBack)    ///<界面初始化
				{
					tMyLable.uiValue = &i;
					tMyLable.bEnable = false;
					tMyLable.uiLimitUp = 1;
					tMyLable.uiLimitDown = 0;
					tMyLable.uiStepValue = 1;
					tMyLable.cHeader = LANG_SOFTWARE_DES;
					for(j = 0; j < 16; j++)
					{
						wcString[j] = mTarget.tProductInfor.ucSoftDescription[j];
					}
					wcString[16] = 0;
					tMyLable.cLeft = wcString;
					tMyLable.cRight = L"";
					SetLableShow(&tMyLable, true, false);
				}
				else
				{
					SetLableShow(&tMyLable, false, false);
				}
			}
			///<主菜单软件版本
			else
			{
				mDrawing->DrawString(&mPaint, LANG_SOFTWAREMENU, 0, 121, 16, drawing::TEXT_ALIGN_CENTER);
			}
			break;
	}
	///<子菜单下处理按键事件
	if(bSubMenu)
	{
		bSubMenuBack = true;

		switch(tMyLable.tKeyEvent->ucIndex)
		{
			///<返回按键,子菜单切换为主菜单
			case APP_KEY_KEYESC_INDEX:
				bSubMenu = false;
				break;
			case APP_KEY_KEYPCIO_INDEX:
				*ucMenuIndex = UITHREAD_MENU_MAIN;
				break;
			default:
				break;
		}
	}
	///<主菜单下处理按键事件
	else
	{
		bSubMenuBack = false;
		tKey = GetKey();
		switch(tKey->ucIndex)
		{
			case APP_KEY_KEYUP_INDEX:   ///<切换主菜单
				ucMenu--;
				if(ucMenu == 0xff)
				{
					ucMenu = 2;
				}
				break;
			case APP_KEY_KEYDOWN_INDEX: ///<切换主菜单
				ucMenu++;
				if(ucMenu == 3)
				{
					ucMenu = 0;
				}
				break;
			case APP_KEY_KEYSET_INDEX:  ///<进入子菜单
				bSubMenu = true;
				bSubMenuBack = false;
				break;
			case APP_KEY_KEYESC_INDEX:
				*ucMenuIndex = UITHREAD_MENU_SETMAINMENU;///<进入系统菜单
				break;
			case APP_KEY_KEYPCIO_INDEX: ///<跳出界面
				*ucMenuIndex = UITHREAD_MENU_MAIN;
				break;
			default:
				break;
		}
	}
}

static void ShowProtectForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
	const uint32_t uiLongDelayTimeTbl[] = {5, 10, 20, 30};
	static MyLableTPDF tMyLable;
	static uint8_t ucMenu, ucMenuBack;
	static wchar_t wcString[16], wcString2[16];
	static uint32_t i, k;
	static float j;
	int buf;

	if(bInit)
	{
		ucMenu = 0;
		ucMenuBack = 1;
		tMyLable.bEnable = false;
		mPaint.Invert = true;
		mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
	}
	memset(wcString2,0,sizeof(wcString2));
	memset(wcString,0,sizeof(wcString));
	mPaint.Invert = false;
	if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_MOTOR)
	{
		switch(ucMenu)
		{
			case 0:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usLongDelayCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_LGDL_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ir(%.2fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 1:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usLongDelayTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_LIST;
					tMyLable.uiList = uiLongDelayTimeTbl;
					tMyLable.ucListLen = sizeof(uiLongDelayTimeTbl) >> 2;
					tMyLable.cHeader = LANG_LGDL_T_DES;
				}
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = L"Class:";
				tMyLable.cRight = wcString2;
				break;
			case 2:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usShortDelayEnable;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_STDL_EN_DES;
				}
				tMyLable.cLeft = LANG_STATE;
				tMyLable.cRight = i ? LANG_OPEN : LANG_CLOSE;
				break;
			case 3:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usShortCircuitCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_STCC_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ii(%.1fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 4:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usBlockCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_BLK_C_DES;
				}
				k = (i * 100) / mTarget.tLcdCustomConfig.usLongDelayCurrent;
				j = ((float)k) / 100.0f;
				if(mTarget.tLcdCustomConfig.usLongDelayEnable)
				{
					mDrawing->font_swprintf(wcString, "Ijam(%.1fIr) ", j);
				}
				else
				{
					mDrawing->font_swprintf(wcString, "Ijam(%.1fIn) ", j);
				}
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 5:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usBlockTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_BLK_T_DES;
				}
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = L"Tjam(S):";
				tMyLable.cRight = wcString2;
				break;
			case 6:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usUnbalanceRated;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_UNB_C_DES;
				}

				j = ((float)i) / 100.0f;
				mDrawing->font_swprintf(wcString2, "%.1f", j);
				tMyLable.cLeft = L"Iu:";
				tMyLable.cRight = wcString2;
				break;
			case 7:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usUnbalanceTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_UNB_T_DES;
				}
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = L"Tu(s):";
				tMyLable.cRight = wcString2;
				break;
			case 8:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usEarthCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_EARTH_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ig(%.2fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 9:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usEarthTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_EARTH_T_DES;
				}
				j = ((float)i) / 1000.0f;
				mDrawing->font_swprintf(wcString2, "%.1f", j);
				tMyLable.cLeft = L"Tg(S):";
				tMyLable.cRight = wcString2;
				break;
		}
	}
	else
	{
		switch(ucMenu)
		{
			case 0:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usLongDelayCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_LGDL_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ir(%.2fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 1:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usLongDelayTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_LGDL_T_DES;
				}
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = L"Tr(6Ir S)";
				tMyLable.cRight = wcString2;
				break;
			case 2:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usShortDelayCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_STDL_C_DES;
				}
				k = (i * 100) / mTarget.tLcdCustomConfig.usLongDelayCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Isd:%.1fxIr ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 3:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usShortDelayTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_STDL_T_DES;
				}
				j = ((float)i) / 1000.0f;
				buf = mDrawing->font_swprintf(wcString2, "%.1f", j);
				tMyLable.cLeft = L"Tsd(S): ";
				tMyLable.cRight = wcString2;
				break;
			case 4:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usShortCircuitCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_STCC_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ii(%.1fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 5:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usNCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_NC_C_DES;
				}
				j = ((float)i) / 10.0f;
				mDrawing->font_swprintf(wcString2, "%.1fxIn", j);
				tMyLable.cLeft = L"In(xIn)";
				tMyLable.cRight = wcString2;
				break;
			case 6:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usEarthCurrent;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_EARTH_C_DES;
				}
				k = (i * 100) / mTarget.tProductInfor.usCurrent;
				j = ((float)k) / 100.0f;
				mDrawing->font_swprintf(wcString, "Ig(%.2fIn) ", j);
				buf = mDrawing->font_swprintf(wcString2, "%d", i);
				tMyLable.cLeft = wcString;
				tMyLable.cRight = wcString2;
				break;
			case 7:
				if(ucMenuBack != ucMenu)
				{
					i = mTarget.tLcdCustomConfig.usEarthTime;
					tMyLable.tMyLableShowType = MYLABLE_SHOWTYPE_TEXT;
					tMyLable.cHeader = LANG_EARTH_T_DES;
				}
				j = ((float)i) / 1000.0f;
				mDrawing->font_swprintf(wcString2, "%.1f", j);
				tMyLable.cLeft = L"Tg(S):";
				tMyLable.cRight = wcString2;
				break;
		}
	}
	if(ucMenuBack != ucMenu)
	{
		ucMenuBack = ucMenu;
		SetLableShow(&tMyLable, true, false);
	}
	else
	{
		SetLableShow(&tMyLable, false, false);
	}
	switch(tMyLable.tKeyEvent->ucIndex)
	{
		case APP_KEY_KEYDOWN_INDEX:
			if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_MOTOR)
			{
				ucMenu++;
				ucMenu %= 10;
			}
			else
			{
				if(ucMenu == 4 && (!(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_4WIRE)))
				{
					ucMenu = 6;
				}
				else
				{
					ucMenu++;
					ucMenu %= 8;
				}
			}
			memset(wcString2,0,sizeof(wcString2));
			memset(wcString,0,sizeof(wcString));
			break;
		case APP_KEY_KEYUP_INDEX:
			if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_MOTOR)
			{
				ucMenu--;
				if(ucMenu == 0xff)
				{
					ucMenu = 9;
				}
			}
			else
			{
				if(ucMenu == 6 && (!(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_4WIRE)))
				{
					ucMenu = 4;
				}
				else
				{
					ucMenu--;
					if(ucMenu == 0xff)
					{
						ucMenu = 7;
					}
				}
			}
			memset(wcString2,0,sizeof(wcString2));
			memset(wcString,0,sizeof(wcString));
			break;
		case APP_KEY_KEYPCIO_INDEX:
		case APP_KEY_KEYESC_INDEX:
			*ucMenuIndex = UITHREAD_MENU_MAIN;
			break;
		default:
			break;
	}
}

static void ShowRecordForm(uint8_t *ucMenuIndex, uint16_t usTick, bool bInit)
{
    const wchar_t *wcShowString[21] =
    {
        L"  N相堵转\0", L"N相不平衡\0", L"N相长延时\0", L"N相短延时\0", L"  N相短路\0",
        L"  C相堵转\0", L"C相不平衡\0", L"C相长延时\0", L"C相短延时\0", L"  C相短路\0",
        L"  B相堵转\0", L"B相不平衡\0", L"B相长延时\0", L"B相短延时\0", L"  B相短路\0",
        L"  A相堵转\0", L"A相不平衡\0", L"A相长延时\0", L"A相短延时\0", L"  A相短路\0",
        L" 接地故障\0"
    };
    const wchar_t *wcShowString_En[5] = {L"Ijam", L"  Iu", L"  Ir", L" Isd", L"  Ii"};
    const wchar_t *wcShowString_En2[4] = {L"PH:N   ", L"PH:C   ", L"PH:B   ", L"PH:A   "};
    const uint16_t usShowCode[21] =
    {
        0x1040, 0x1080, 0x1200, 0x1400, 0x1800,
        0x2040, 0x2080, 0x2200, 0x2400, 0x2800,
        0x4040, 0x4080, 0x4200, 0x4400, 0x4800,
        0x8040, 0x8080, 0x8200, 0x8400, 0x8800,
        0x0100
    };
    static uint8_t ucMenu;
    static wchar_t wcString1[20], wcString2[20];
    KeyEventTPDF* tKey;
    uint8_t i;
    int buf;

    if(bInit)
    {
        ucMenu = 0;
        mPaint.Invert = true;
        mDrawing->DrawRectangle(&mPaint, 0, 0, 120, 31);
        memset(wcString1, 0, sizeof(wcString1));
        memset(wcString2, 0, sizeof(wcString2));
    }
    mPaint.Invert = false;
		if(mTarget.tSecurityConfig.usLanguage)
    {
        mDrawing->font_swprintf(wcString1, "Record %d: ", ucMenu + 1);
        mDrawing->DrawString(&mPaint, wcString1, 0, 121, 0, drawing::TEXT_ALIGN_LEFT);
        for(i = 0; i < 21; i++)
        {
            if(mTarget.tFaultRecord[ucMenu].usReason == usShowCode[i])
            {
                break;
            }
        }
        if(i == 21)
        {
            mDrawing->DrawString(&mPaint, L"     None", 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
            mDrawing->DrawString(&mPaint, L"PH:None", 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
            mDrawing->DrawString(&mPaint, L"         ", 0, 121, 16, drawing::TEXT_ALIGN_RIGHT);
        }
        else if(i == 20)
        {
            mDrawing->DrawString(&mPaint, L"  Ig", 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
            mDrawing->DrawString(&mPaint, L"PH:GND ", 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
            mDrawing->font_swprintf(wcString2, "I:%dA", (uint32_t)mTarget.tFaultRecord[ucMenu].usCurrent);
            mDrawing->DrawString(&mPaint, wcString2, 0, 121, 16, drawing::TEXT_ALIGN_RIGHT);
        }
        else
        {
            mDrawing->DrawString(&mPaint, wcShowString_En[i % 5], 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
            mDrawing->DrawString(&mPaint, wcShowString_En2[i / 5], 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
            mDrawing->font_swprintf(wcString2, "I:%dA", (uint32_t)mTarget.tFaultRecord[ucMenu].usCurrent);
            mDrawing->DrawString(&mPaint, wcString2, 0, 121, 16, drawing::TEXT_ALIGN_RIGHT);
        }
    }
    else
    {
			mDrawing->DrawString(&mPaint, LANG_CH_ERR_BEFOR,0, 121, 0, drawing::TEXT_ALIGN_LEFT);
			mDrawing->font_swprintf(wcString1, "%d", ucMenu + 1);
			mDrawing->DrawString(&mPaint, wcString1, 12,121 , 0, drawing::TEXT_ALIGN_LEFT);
			for(i = 0; i < 21; i++)
			{
				if(mTarget.tFaultRecord[ucMenu].usReason == usShowCode[i])
				{
					break;
				}
			}
			if(i == 21)
			{
					mDrawing->DrawString(&mPaint, L"       无", 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
			}
			else
			{
					mDrawing->DrawString(&mPaint, wcShowString[i], 0, 121, 0, drawing::TEXT_ALIGN_RIGHT);
			}
			mDrawing->DrawString(&mPaint, L"故障电流(A):", 0, 121, 16, drawing::TEXT_ALIGN_LEFT);
			buf = mDrawing->font_swprintf(wcString2, "%05dA", (uint32_t)mTarget.tFaultRecord[ucMenu].usCurrent);
			ZeroClear(wcString2, buf);
			mDrawing->DrawString(&mPaint, wcString2, 0, 121, 16, drawing::TEXT_ALIGN_RIGHT);
    }
    tKey = GetKey();
    switch(tKey->ucIndex)
    {
        case APP_KEY_KEYPCIO_INDEX:
        case APP_KEY_KEYESC_INDEX:
            *ucMenuIndex = UITHREAD_MENU_MAIN;
            break;
        case APP_KEY_KEYDOWN_INDEX:
            ucMenu++;
            ucMenu %= FAULT_RECORD_LEN;
            break;
        case APP_KEY_KEYUP_INDEX:
            ucMenu--;
            if(ucMenu == 0xff)
            {
                ucMenu = FAULT_RECORD_LEN - 1;
            }
            break;
        default:
            break;
    }
}

static void ShowLog(void)
{
	const unsigned char *ImgPtr = (_PRODUCT_ATTRIBUTE & CONFIG_ATTRIBUTE_FLAG_NOARK)?mMyLogNoark:mMyLogChint;
	mLogImage->Stream = ImgPtr;
	mDrawing->DrawImage(mLogImage, 0, 0);
	mLogImage->Stream = ImgPtr + 128;
	mDrawing->DrawImage(mLogImage, 0, 8);
	mLogImage->Stream = ImgPtr + 256;
	mDrawing->DrawImage(mLogImage, 0, 16);
	mLogImage->Stream = ImgPtr + 384;
	mDrawing->DrawImage(mLogImage, 0, 24);
	Method::Sleep(1000);
	mDrawing->Clear();
}

static void UiHardwareInit(void)
{
	lcd_cmf1p2301::LcdCmf1p2301InitTPDF init =
	{
		{APP_LCD_CD_PORT, 	APP_LCD_CD_PIN},
		{APP_LCD_CS0_PORT, 	APP_LCD_CS0_PIN},
		{APP_LCD_LCDR_PORT, APP_LCD_LCDR_PIN},
		{APP_LCD_WR0_PORT, 	APP_LCD_WR0_PIN},
		{APP_LCD_WR1_PORT, 	APP_LCD_WR1_PIN},
		{APP_LCD_LSB_PORT, 	APP_LCD_LSB_PIN},
	};
	mLcd = new lcd_cmf1p2301(&init);
	mDrawing = new drawing(mLcd);
	mLcd->Reset();
	mLcd->Clear();
	mDrawing->InitFontModule((uint32_t)&MyFontModule);

	mKeys[0] 	= new key(APP_KEY_KEYUP_INDEX,		APP_KEY_KEYUP_PORT, 	APP_KEY_KEYUP_PIN,		true,	50,	true,	1000);
	mKeys[1] 	= new key(APP_KEY_KEYDOWN_INDEX,	APP_KEY_KEYDOWN_PORT, APP_KEY_KEYDOWN_PIN,	true,	50,	true,	1000);
	mKeys[2] 	= new key(APP_KEY_KEYESC_INDEX,		APP_KEY_KEYESC_PORT, 	APP_KEY_KEYESC_PIN,		true,	50,	false);
	mKeys[3] 	= new key(APP_KEY_KEYSET_INDEX,		APP_KEY_KEYSET_PORT, 	APP_KEY_KEYSET_PIN,		true,	50,	false);
	mKeys[4] 	= new key(APP_KEY_KEYPCIO_INDEX,	APP_KEY_KEYPCIO_PORT, APP_KEY_KEYPCIO_PIN,	true,	50,	false);
	key::Register(mKeys[0]);
	key::Register(mKeys[1]);
	key::Register(mKeys[2]);
	key::Register(mKeys[3]);
	key::Register(mKeys[4]);
	
	mCoordinateImage = new image();
	mCoordinateImage->DrawingDirect = image::DRAWINGDIRECT_VERTICAL;
	mCoordinateImage->ColorType = lcd::COLOR_TYPE_MONOCHROME;
	mCoordinateImage->Width = 120;
	mCoordinateImage->Height = 16;
	mCoordinateImage->Stream = mMyCoordinate;
	
	mLogImage = new image();
	mLogImage->DrawingDirect = image::DRAWINGDIRECT_VERTICAL;
	mLogImage->ColorType = lcd::COLOR_TYPE_MONOCHROME;
	mLogImage->Width = 120;
	mLogImage->Height = 8;
	mLogImage->Stream = mMyLogNoark;
	
	mPaint.Color.Blue = 0xffff;
	mPaint.Color.Green = 0xffff;
	mPaint.Color.Red = 0xffff;
	mPaint.DashStyle = drawing::DASH_STYLE_SOLID;
	mPaint.Invert = false;
	mPaint.PaintStyle = drawing::PAINT_STYLE_STROKE;
	mPaint.Width = 1;
	
	mLcdSendStream = new stream("lcd/pub", stream::STREAM_DIRECT_PUBLIC, sizeof(WriteCommandTPDF));
	mLcdRecStream = new stream("lcd/sbs", stream::STREAM_DIRECT_SUBSCRIBE, sizeof(WriteStatusTPDF));
	
	mWrtCmd.usLen = 1;
}

static void UiThread(void *args)
{
	const FormPartitionTPDF tFormPartition[] =
	{
		{UITHREAD_MENU_MAIN,                MainForm},
		{UITHREAD_MENU_SETPRESHOW,  SetPreShowForm},
		{UITHREAD_MENU_SETPASSSHOW, SetPassShowForm},
		{UITHREAD_MENU_SETMAINMENU, SetMainMenuForm},
		{UITHREAD_MENU_PROTECTSET,  SetProtectForm},
		{UITHREAD_MENU_COMSET,          SetComForm},
		{UITHREAD_MENU_SYSTEMSET,   SetSystemForm},
		{UITHREAD_MENU_PROTECTSHOW, ShowProtectForm},
		{UITHREAD_MENU_RECORDSHOW,  ShowRecordForm},
	};
	uint8_t ucMenuIndex = UITHREAD_MENU_MAIN;
	uint8_t ucMenuIndexBack = 1;
	uint16_t usTick = 0;
	uint8_t  ucRef =0;
	uint8_t  ucReftemp=0;
	UiHardwareInit();
	/** 显示LOG*/
	if(_PRODUCT_ATTRIBUTE & CONFIG_ATTRIBUTE_FLAG_LOGEN)
	{
		ShowLog();
	}
	while(1)
	{
		Method::Sleep(1);
		usTick = (uint16_t)mLcd->uiGetTick();
		ucRef = usTick /mLcd->GetLcdAttribute()->Refresh;//可以根据需要调节刷新率
		if(ucReftemp != ucRef) 
		{
			ucReftemp = ucRef;
			if( ucMenuIndexBack != ucMenuIndex)
			{
				ucMenuIndexBack = ucMenuIndex;
				mLcd->vSetTick(0);
				usTick = 0;
				tFormPartition[ucMenuIndex].FunctionAddr(&ucMenuIndex, usTick, true);
			}
			else
			{
				tFormPartition[ucMenuIndex].FunctionAddr(&ucMenuIndex, usTick, false);
			}
		}
	}
}

void UiThreadInit(void)
{
	threados *t1 = new threados(UiThread);
	t1->Name = "Ui thread";
	t1->Prio = 0;
	t1->StackDeep = 512;
	t1->Start();
}
